package com.huodongrili.calender.data;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.huodongrili.calender.data.DBTables.Feedback;

public class FeedbackData {

	private Context mContext;

	public FeedbackData(Context context) {
		mContext = context.getApplicationContext();
	}
	
	
	public void addFeedback(String content,long date){
		SQLiteDatabase db = DBHelper.getWriteDatabase(mContext);
		
		ContentValues cv = new ContentValues();
		cv.put(Feedback.CONTENT, content);
		cv.put(Feedback.DATELONG, date);
		
		db.insert(Feedback.TABLE_NAME, null, cv);

	}
	
	
	
	public void clearFeedbacks(){
		SQLiteDatabase db = DBHelper.getWriteDatabase(mContext);
		
		String sql = "delete from " + Feedback.TABLE_NAME;
		db.execSQL(sql);
		
	}
	
	
	

	public List<String> getFeedbacks() {

		SQLiteDatabase db = DBHelper.getReadDatabase(mContext);

		Cursor cursor = null;
		List<String> feedbacks = null;

		try {

			cursor = db.rawQuery("select * from " + Feedback.TABLE_NAME, null);
			int count = cursor.getCount();
			if (cursor != null && count > 0) {
				feedbacks = new ArrayList<String>(count);
				cursor.moveToFirst();
				while (!cursor.isAfterLast()) {
					String content = cursor.getString(cursor
							.getColumnIndex(Feedback.CONTENT));
					/*long date= cursor.getLong(cursor
							.getColumnIndex(Feedback.DATELONG));*/

					feedbacks.add(content);
					
					cursor.moveToNext();
				}

			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			if (cursor != null) {
				cursor.close();
			}
		}
		
		return feedbacks;

	}

}
